package info.liuxing.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * HttpConnectionUtil
 *
 * @author liuxing (SE)
 * @summary HttpConnectionUtil
 * @Copyright (c) 2017, Lianjia Group All Rights Reserved.
 * @since 2017-12-05 00:16
 */
public class HttpConnectionUtil {

    public static String doGet() {
        HttpURLConnection httpConn = null;
        BufferedReader reader = null;

        try {
            URL postUrl = new URL("http://www.baidu.com");
            httpConn = (HttpURLConnection) postUrl.openConnection();
            httpConn.setRequestMethod("GET");
            httpConn.setUseCaches(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestProperty("Accept-Encoding", "gzip,deflate");

            httpConn.connect();

            reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            StringBuilder builder = new StringBuilder();

            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            httpConn.disconnect();
        }

        return null;
    }

    public static void main(String[] args) {
        System.out.println(doGet());
    }
}
